package com.huarui.returned.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.returned.dao.IReturnedItemDao;
import com.huarui.returned.model.ReturnedItem;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class ReturnedItemDaoImpl extends BaseDaoImpl implements IReturnedItemDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,String>> getReturnedItemsByReturnedId(Integer returnedId) {
		
		return  getSqlMapClientTemplate().queryForList(namespace+".getReturnedItemsByReturnedId", returnedId);
	}

	@Override
	public void batchInsertReturnedItems(final List<ReturnedItem> returnedItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				for(ReturnedItem returnedItem : returnedItems){
					executor.insert(namespace+".create", returnedItem);					
				}
				executor.executeBatch();
				return null;
			}
		});
		
	}

	@Override
	public void batchUpdateReturnedItems(final List<ReturnedItem> returnedItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(ReturnedItem returnedItem : returnedItems){
					executor.update(namespace+".update", returnedItem);
					batch++;
					if(batch == 100){
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	@Override
	public Integer batchDeleteReturnedItems(Map<String,Object> map) {
		/*getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				for(Integer deleteItemId : deleteItemIds){
					executor.delete(namespace+".deleteById", deleteItemId);
				}
				executor.executeBatch();
				return null;
			}
		});*/
		return getSqlMapClientTemplate().delete(namespace+".deleteByIds", map);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Integer getReturnedItemCountByConditions(Map queryMap) {
		
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getReturnedItemCountByConditions", queryMap);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getReturnedItemByConditions(Map queryMap) {
		return getSqlMapClientTemplate().queryForList(namespace+".getReturnedItemByConditions", queryMap);
		
	}



}
